/****   http.js   ****/
// 导入封装好的axios实例
import request from "./request";
const isDev = import.meta.env.MODE === "development";

const PROD_PREFIX = "/prod";
const API_PROD_PREFIX = "/apiprod";
const PROD_ENDPOINTS = ["/robot/new"];

const http = {
  /**
   * methods: 请求
   * @param url 请求地址
   * @param params 请求参数
   */
  // get(url, params) {
  //     const config = {
  //         method: 'get',
  //         url: url
  //     }
  //     if (params) config.params = params
  //     return request(config)
  // },
  get(url, params) {
    url = this.processUrl(url);
    const filteredParams =
      params == null
        ? null
        : Object.keys(params).reduce((acc, key) => {
            if (params[key] !== undefined && params[key] !== "") {
              acc[key] = params[key];
            }
            return acc;
          }, {});
    const config = {
      method: "get",
      url: url,
      params: filteredParams,
    };
    return request(config);
  },
  post(url, params) {
    // 新增路径处理
    url = this.processUrl(url);
    const config = {
      method: "post",
      url: url,
    };
    if (params) config.data = params;
    return request(config);
  },
  put(url, params) {
    const config = {
      method: "put",
      url: url,
    };
    if (params) config.data = params;
    return request(config);
  },
  delete(url, params) {
    const config = {
      method: "delete",
      url: url,
    };
    if (params) config.data = params;
    return request(config);
  },
  processUrl(url) {
    if (isDev) {
      return url;
    }

    if (PROD_ENDPOINTS.includes(url)) {
      return PROD_PREFIX + url;
    }
    return API_PROD_PREFIX + url;
  },
};
//导出
export default http;
